<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="停车场编码" prop="pkGlobalid">
        <el-input
          v-model="queryParams.pkGlobalid"
          placeholder="请输入停车场编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="carLicenseNumber">
        <el-input
          v-model="queryParams.carLicenseNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:parkCarParking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:parkCarParking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:parkCarParking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:parkCarParking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkCarParkingList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆和车场关系表" align="center" prop="id" v-if="false"/>
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="车主ID，由外部平台生成的唯一编号" align="center" prop="customerId" />
      <el-table-column label="停车场编码" align="center" prop="pkGlobalid" />
      <el-table-column label="车牌号" align="center" prop="carLicenseNumber" />
      <el-table-column label="车主在vems的递增序列号" align="center" prop="customerSeq" />
      <el-table-column label="VIP客户角色，0无角色，用于扩充用户角色使用" align="center" prop="customerRole" />
      <el-table-column label="VIP客户拥有的VIP车辆数量" align="center" prop="carNumber" />
      <el-table-column label="carportNumber" align="center" prop="carportNumber" />
      <el-table-column label="房间号" align="center" prop="customerRoomNumber" />
      <el-table-column label="0未启用，1启用，2禁用" align="center" prop="customerStatus" :formatter="customerStatusFormat" />
      <el-table-column label="是否逻辑删除，0未删除，1删除" align="center" prop="isDeleted" :formatter="isDeletedFormat" />
      <el-table-column label="是否逻辑删除，0未删除，1删除" align="center" prop="updatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否逻辑删除，0未删除，1删除" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:parkCarParking:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:parkCarParking:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆和车场关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true"  label-width="120px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="车主ID，由外部平台生成的唯一编号" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入车主ID，由外部平台生成的唯一编号" />
        </el-form-item>
        <el-form-item label="停车场编码" prop="pkGlobalid">
          <el-input v-model="form.pkGlobalid" placeholder="请输入停车场编码" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carLicenseNumber">
          <el-input v-model="form.carLicenseNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车主在vems的递增序列号" prop="customerSeq">
          <el-input v-model="form.customerSeq" placeholder="请输入车主在vems的递增序列号" />
        </el-form-item>
        <el-form-item label="VIP客户角色，0无角色，用于扩充用户角色使用" prop="customerRole">
          <el-input v-model="form.customerRole" placeholder="请输入VIP客户角色，0无角色，用于扩充用户角色使用" />
        </el-form-item>
        <el-form-item label="VIP客户拥有的VIP车辆数量" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入VIP客户拥有的VIP车辆数量" />
        </el-form-item>
        <el-form-item label="carportNumber" prop="carportNumber">
          <el-input v-model="form.carportNumber" placeholder="请输入carportNumber" />
        </el-form-item>
        <el-form-item label="房间号" prop="customerRoomNumber">
          <el-input v-model="form.customerRoomNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="0未启用，1启用，2禁用">
          <el-radio-group v-model="form.customerStatus">
            <el-radio
              v-for="dict in customerStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否逻辑删除，0未删除，1删除">
          <el-radio-group v-model="form.isDeleted">
            <el-radio
              v-for="dict in isDeletedOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否逻辑删除，0未删除，1删除" prop="updatetime">
          <el-date-picker clearable size="small"
            v-model="form.updatetime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择是否逻辑删除，0未删除，1删除">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否逻辑删除，0未删除，1删除" prop="createtime">
          <el-date-picker clearable size="small"
            v-model="form.createtime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择是否逻辑删除，0未删除，1删除">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParkCarParking, getParkCarParking, delParkCarParking, addParkCarParking, updateParkCarParking, exportParkCarParking } from "@/api/system/parkCarParking";

export default {
  name: "ParkCarParking",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆和车场关系表格数据
      parkCarParkingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 0未启用，1启用，2禁用字典
      customerStatusOptions: [],
      // 是否逻辑删除，0未删除，1删除字典
      isDeletedOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        customerId: undefined,
        pkGlobalid: undefined,
        carLicenseNumber: undefined,
        customerSeq: undefined,
        customerRole: undefined,
        carNumber: undefined,
        carportNumber: undefined,
        customerRoomNumber: undefined,
        customerStatus: undefined,
        isDeleted: undefined,
        updatetime: undefined,
        createtime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("car_parking_customer_status").then(response => {
      this.customerStatusOptions = response.data;
    });
    this.getDicts("sys_is_delete").then(response => {
      this.isDeletedOptions = response.data;
    });
  },
  methods: {
    /** 查询车辆和车场关系列表 */
    getList() {
      this.loading = true;
      listParkCarParking(this.queryParams).then(response => {
        this.parkCarParkingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 0未启用，1启用，2禁用字典翻译
    customerStatusFormat(row, column) {
      return this.selectDictLabel(this.customerStatusOptions, row.customerStatus);
    },
    // 是否逻辑删除，0未删除，1删除字典翻译
    isDeletedFormat(row, column) {
      return this.selectDictLabel(this.isDeletedOptions, row.isDeleted);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        customerId: undefined,
        pkGlobalid: undefined,
        carLicenseNumber: undefined,
        customerSeq: undefined,
        customerRole: undefined,
        carNumber: undefined,
        carportNumber: undefined,
        customerRoomNumber: undefined,
        customerStatus: 0,
        isDeleted: 0,
        updatetime: undefined,
        createtime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆和车场关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getParkCarParking(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆和车场关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateParkCarParking(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParkCarParking(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除车辆和车场关系编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delParkCarParking(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有车辆和车场关系数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportParkCarParking(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
